package com.gitee.wsl.compose.state


import androidx.compose.runtime.Composable
import androidx.compose.runtime.mutableDoubleStateOf
import androidx.compose.runtime.mutableFloatStateOf
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableLongStateOf
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateMapOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.runtime.toMutableStateList
import androidx.compose.runtime.toMutableStateMap

object S{

    @Composable
    fun state(default: Double ) = remember {
        mutableDoubleStateOf(default)
    }

    @Composable
    fun state(default: Float ) = remember {
        mutableFloatStateOf(default)
    }

    @Composable
    fun state(default: Int ) = remember {
        mutableIntStateOf(default)
    }

    @Composable
    fun state(default: Long ) = remember {
        mutableLongStateOf(default)
    }

    @Composable
    fun <T> state(init:T) = remember {
        mutableStateOf(init)
    }

    @Composable
    fun <T> state(init:()->T) = remember {
        mutableStateOf(init())
    }

    @Composable
    fun <T> state(vararg elements: T) = remember {
        mutableStateListOf(elements)
    }

    @Composable
    fun <T> state(elements: Collection<T>) = remember {
        elements.toMutableStateList()
    }

    @Composable
    fun <K, V> state(vararg pairs: Pair<K, V>) = remember {
        mutableStateMapOf(*pairs)
    }

    @Composable
    fun <K, V> state(pairs: Iterable<Pair<K, V>>) = remember {
        pairs.toMutableStateMap()
    }

    @Composable
    fun <T> stateBy(key1: Any?, value: T) =
        remember(key1 = key1) { mutableStateOf(value) }

    @Composable
    fun <T> stateBy(key1: Any?, key2: Any?, value: T) =
        remember(key1 = key1, key2 = key2) { mutableStateOf(value) }

    @Composable
    fun <T> stateBy(key1: Any?, key2: Any?, key3: Any?, value: T) =
        remember(key1 = key1, key2 = key2, key3 = key3) { mutableStateOf(value) }

    @Composable
    fun <T> rememberMutableStateListOf(): SnapshotStateList<T> = remember { SnapshotStateList() }

    /**
     * 和mutableStateOf有什么不同, 不会触发重组
     */
    //@Composable
    //fun<T> ref(value:T) = remember { Ref(value) }
}




